<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Create extends TTA_Controller {
	public function __construct() {
		Modules::run('login/admin/check_logged_in');	
		$this->load->model('functions_model');
	}
	
	public function index()
	{
		//$this->template->build('create_article');
	}
	
	/* 
	  function use to create menu car that include brand car and model car
	*/
	public function create_menu_car() {
		$results = $this->functions_model->get_car();
		
		$brand_id = 0;
		$carlist = new SimpleXMLElement('<carmenu/>');
		$brand = NULL;
		foreach($results as $record) {
			if($record['brand_id'] != $brand_id) {
				$brand_id = $record['brand_id'];
				$brand = $carlist->addChild('brand');
				$brand->addAttribute('id', $brand_id);
				$brand->addAttribute('name', $record['brand_name']);
			}
			if($record['model_id'] != NULL) {
				$model = $brand->addChild('model');
				$model->addAttribute('id', $record['model_id']);
				$model->addAttribute('name', $record['model_name']);
			}
		}
		$path =  str_replace('\\', '/', FCPATH);
		$carlist->asXML($path . 'data/menucar.xml');
	}
	
	/*
		function to create country menu
	*/
	public function create_menu_country() {
		$source = $this->functions_model->get_country();
		$this->create_simple_menu($source, 'countries', 'country', 
			array('id' => 'country_id', 'name' => 'country_name'), 'data/menucountry.xml');
	}
	
	/*
		function to create fuel menu
	*/
	public function create_menu_fuel() {
		$source = $this->functions_model->get_fuel();
		$this->create_simple_menu($source, 'fuels', 
			'fuel', array('id' => 'fuel_id', 'name' => 'fuel_name'), 'data/menufuel.xml');
	}
	
	/*
		function to create a simple menu
	*/
	private function create_simple_menu($source, $menu_name, $element_name, $children_arr, $save_path) {
		$list = new SimpleXMLElement('<' . $menu_name . '/>');
		foreach($source as $record) {
			$element = $list->addChild($element_name);
			foreach($children_arr as $key => $value) {
				$element->addChild($key, $record[$value]);
			}
		}
		$path = str_replace('\\', '/', FCPATH);
		$list->asXML($path . $save_path);
	}
}

/* End of file create.php */
/* Location: ./application/modules/admin_functions/controllers/create.php */